package br.com.utilitarios;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.Dao.UsuarioClienteDao;
import br.com.Modelo.UsuarioCliente;

/**
 * Servlet Filter implementation class filter
 */
@WebFilter("/user_comerciante.jsp")
public class filterComerciante implements Filter {

    /**
     * Default constructor. 
     */
	private ServletContext context;
    public filterComerciante() {
        // TODO Auto-generated constructor stub
    	
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		// String url = ((HttpServletRequest) request).getRequestURI(); 
		// System.out.print(url);
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			
			/*String url = req.getRequestURI();
			this.context.log("Requested Resource::"+url);
			
			
			UsuarioCliente user = null;
			
             UsuarioClienteDao dao = new UsuarioClienteDao();
             HttpSession session = req.getSession();     
             
             System.out.print(session);
             if(session != null){
            	
             
             String id = (String) session.getAttribute("user");
             
             
             System.out.print(id);
             
             if(id != null){
            	 user = dao.validaAutenticacaoUser(Long.parseLong(id));
			
                   //System.out.print("autenticaçao do usuario"+user.getAutenticacao());
             
                  if(user.getAutenticacao() == 2){
            	 
            		this.context.log("Unauthorized access request");
            		 System.out.print("autenticaçao do usuario  dsadasdas --- "+user.getAutenticacao());
    				res.sendRedirect("index.jsp");
    				//session = null;
                   }
             }else{
            	 
            	 this.context.log("Unauthorized access request");
     			res.sendRedirect("notpage.jsp");
             }
			
             		if(session == null && !(url.endsWith(".jsp") || url.endsWith("LoginServlet"))){
             			    	this.context.log("Unauthorized access request");
             					res.sendRedirect("index.jsp");
						}else{
				// pass the request along the filter chain
								chain.doFilter(request, response);
						}
             }*/
            	 
			chain.doFilter(request, response);
             
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		this.context = fConfig.getServletContext();
		this.context.log("AuthenticationFilter initialized");
	}

}
